<?php

namespace App\Containers\AppSection\Signature\Tasks;

use App\Containers\AppSection\Signature\Data\Repositories\SignatureRepository;
use App\Containers\AppSection\Signature\Models\Signature;
use App\Ship\Exceptions\NotFoundException;
use App\Ship\Parents\Tasks\Task;
use Exception;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;

class DownloadSignatureTask extends Task
{
    protected SignatureRepository $repository;
    protected Storage $storage;

    public function __construct(
        SignatureRepository $repository,
        Storage        $storage
    )
    {
        $this->storage = $storage;
        $this->repository = $repository;
    }

    public function run(int $id): StreamedResponse
    {
        try {
            /** @var Signature $signature */
            $signature = $this->repository->find($id);

            return $this->storage::disk(config('filesystems.cloud'))->download($signature->path);

        } catch (Exception $exception) {
            throw new NotFoundException(__('ship::resource.not-found'));
        }
    }
}
